package com.thb.pay.manage.web.agent;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thb.pay.api.agent.AgentAgentpayPassageServiceApi;
import com.thb.pay.api.agent.AgentInfoServiceApi;
import com.thb.pay.api.agent.vo.req.AgentAgentpayPassagePageReq;
import com.thb.pay.api.agent.vo.req.AgentAgentpayPassageReq;
import com.thb.pay.api.agent.vo.resp.AgentAgentpayPassageVo;
import com.thb.pay.api.agent.vo.resp.AgentInfoVo;
import com.thb.pay.api.payment.AgentpayPassageServiceApi;
import com.thb.pay.api.payment.vo.resp.AgentpayPassageVo;
import com.thb.pay.core.annotation.OperationLog;
import com.thb.pay.core.utils.R;
import com.thb.pay.manage.comm.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @类名称 AgentAgentpayPassageController
 * @类描述 <pre>代理商代付通道信息控制类</pre>
 * @作者 lucky
 * @创建时间 2019/10/12
 * @版本 v1.01
 * </pre>
 */
@Slf4j
@RestController
@RequestMapping("api/sys/agentAgentpayPassage")
public class AgentAgentpayPassageController extends BaseController {


    @Autowired
    private AgentAgentpayPassageServiceApi agentAgentpayPassageServiceApi;

    @Autowired
    private AgentpayPassageServiceApi agentpayPassageServiceApi;

    @Autowired
    private AgentInfoServiceApi agentInfoServiceApi;

    @PostMapping("/list")
    public R list(@RequestBody AgentAgentpayPassagePageReq pageReq) {
        List<AgentAgentpayPassageVo> resultDataList = Lists.newArrayList();
        AgentInfoVo agentInfoVo = agentInfoServiceApi.getByAgentId(pageReq.getAgentId());
        if (agentInfoVo == null) {
            return R.ok(resultDataList);
        }
        List<AgentAgentpayPassageVo> agentAgentpayPassageVos = agentAgentpayPassageServiceApi.findListByAgentId(pageReq.getAgentId());
        Map<Long, AgentAgentpayPassageVo> agentAgentpayPassageVoMap = Maps.newHashMap();
        for (AgentAgentpayPassageVo agentAgentpayPassageVo : agentAgentpayPassageVos) {
            agentAgentpayPassageVoMap.put(agentAgentpayPassageVo.getAgentpayPassageId(), agentAgentpayPassageVo);
        }

        List<AgentpayPassageVo> agentpayPassageVos = agentpayPassageServiceApi.findAllList();
        Map<Long, AgentpayPassageVo> agentpayPassageVoMap = Maps.newHashMap();
        for (AgentpayPassageVo agentpayPassageVo : agentpayPassageVos) {
            agentpayPassageVoMap.put(agentpayPassageVo.getId(), agentpayPassageVo);
        }

        agentpayPassageVoMap.keySet().forEach(passageId -> {
            AgentpayPassageVo agentpayPassageVo = agentpayPassageVoMap.get(passageId);
            AgentAgentpayPassageVo agentAgentpayPassageVo = agentAgentpayPassageVoMap.get(passageId);
            if (agentAgentpayPassageVo == null) {
                agentAgentpayPassageVo = new AgentAgentpayPassageVo();
                agentAgentpayPassageVo.setAgentId(pageReq.getAgentId());
                agentAgentpayPassageVo.setAgentpayPassageId(passageId);
                agentAgentpayPassageVo.setStatus(0);//未添加该通道/
            }
            agentAgentpayPassageVo.setAgentpayPassageName(agentpayPassageVo.getName());
            resultDataList.add(agentAgentpayPassageVo);
        });
        return R.ok(resultDataList);
    }

    /**
     * 查询
     *
     * @param agentId           代理商id
     * @param agentpayPassageId 代付通道id
     * @return
     */
    @GetMapping("/get")
    public R get(@RequestParam("agentId") Long agentId, @RequestParam("agentpayPassageId") Long agentpayPassageId) {
        AgentInfoVo agentInfoVo = agentInfoServiceApi.getByAgentId(agentId);
        if (agentInfoVo == null) {
            return R.ok();
        }
        AgentpayPassageVo agentpayPassageVo = agentpayPassageServiceApi.getById(agentpayPassageId);
        if (agentpayPassageVo == null) {
            return R.ok();
        }
        AgentAgentpayPassageVo agentAgentpayPassageVo = agentAgentpayPassageServiceApi.getByAgentIdAndPassageId(agentId, agentpayPassageId);
        if (agentAgentpayPassageVo == null) {
            agentAgentpayPassageVo = new AgentAgentpayPassageVo();
            agentAgentpayPassageVo.setAgentId(agentId);
            agentAgentpayPassageVo.setAgentpayPassageId(agentpayPassageId);
            agentAgentpayPassageVo.setStatus(0);
        }
        agentAgentpayPassageVo.setAgentpayPassageName(agentpayPassageVo.getName());
        return R.ok(agentAgentpayPassageVo);
    }

    @PostMapping("/addOrUpdate")
    @OperationLog(value = "添加代理商代付通道")
    public R addOrUpdate(@RequestBody AgentAgentpayPassageReq agentAgentpayPassageReq) {
        agentAgentpayPassageServiceApi.addOrUpdate(agentAgentpayPassageReq);
        return R.ok();
    }

}
